﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BadHabit.Factory.Data {

    /// <summary>
    /// Implements the business function for task dataset.
    /// </summary>
    public partial class TaskDataSet {



        public partial class TaskDataTable {

            
            /// <summary>
            /// Every time one column change at the task, updates its depedency columns.
            /// </summary>
            /// <param name="e"></param>
            protected override void OnColumnChanged(System.Data.DataColumnChangeEventArgs e) {
                base.OnColumnChanged(e);

                // If status was changed, updates start and finish date
                if (e.Column == StatusColumn) {
                    TaskRow task = e.Row as TaskRow;
                    task.SetFinishAccordingStatus();
                }

                // If TaskAssignee was set to DBNULL, reject it
                // WHY I DO THIS?
                // cuz when this dataset is binded to a combobox member, 
                // it may happen that the member is not present at the comobo items
                // so this avoid the combo to clear its value
                if (e.Column == TaskAssigneeColumn) {
                    if (DBNull.Value.Equals(e.ProposedValue)) {
                        e.Row.RejectChanges();
                    }
                }

                if (e.Column == ActualEffortColumn) {
                    TaskRow task = e.Row as TaskRow;
                    if (task.ActualEffort < 0)
                        throw new System.Data.DataException("Effort should be greather than zero.");
                }

                if (e.Column == PlannedEffortColumn) {
                    TaskRow task = e.Row as TaskRow;
                    if (task.PlannedEffort < 0)
                        throw new System.Data.DataException("Effort should be greather than zero.");
                }

            }
            
        }


        public partial class TaskRow {
            
            /// <summary>
            /// Sets the finish date acording the task status.
            /// </summary>
            public void SetFinishAccordingStatus() {
              
                // if the task was in the same state before                
                if (this.HasVersion(System.Data.DataRowVersion.Current) &&
                    this[tableTask.StatusColumn, System.Data.DataRowVersion.Current].Equals(
                    this[tableTask.StatusColumn, System.Data.DataRowVersion.Proposed]))
                    return;

                switch (Status) {
                    case (short)TaskStatus.CREATION:
                    case (short)TaskStatus.TASK_REQUIRED:
                        SetStartNull();
                        SetFinishNull();
                        break;
                    case (short)TaskStatus.TASK_VALIDATING:
                    case (short)TaskStatus.TASK_WORKINGON:
                        if (IsStartNull())
                            Start = DateTime.Now;
                        SetFinishNull();
                        break;                    
                    case (short)TaskStatus.TASK_DONE:
                    case (short)TaskStatus.TASK_CANCELED:
                        Finish = DateTime.Now;
                        break;
                }
                
            }



          
        }

        
        
    }
}
